package com.lyn.android_custom_view;

import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.Nullable;

import com.lyn.android_custom_view.databinding.ActivityMainBinding;
import com.lyn.android_custom_view.databinding.AdapterItemBinding;
import com.lyn.module_custom_base.adapter.BaseAdapter;
import com.lyn.module_custom_base.avtivity.BaseActivity;
import com.lyn.module_custom_photo.dialog.SelectPhotoDialog;
import com.lyn.module_custom_utils.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity<ActivityMainBinding> {

    private BaseAdapter<String, AdapterItemBinding> adapter;

    @Override
    public ActivityMainBinding getBinding() {
        return ActivityMainBinding.inflate(getLayoutInflater());
    }

    @Override
    public void setBinding(ActivityMainBinding binding) {
        adapter = new BaseAdapter<String, AdapterItemBinding>() {
            @Override
            protected AdapterItemBinding getViewBinding(ViewGroup parent) {
                return AdapterItemBinding.inflate(getLayoutInflater(), parent, false);
            }

            @Override
            protected void bindData(AdapterItemBinding adapterItemBinding, String s, int position) {
                adapterItemBinding.item.setText(s);
            }

            @Override
            protected void bindListener(AdapterItemBinding adapterItemBinding, String s, int position) {

            }
        };
        List<String> data = new ArrayList<>();
        data.add("统计图");
        data.add("摄像头");
        data.add("图片选中器");
        adapter.setData(data);
        binding.listView.setAdapter(adapter);
    }

    @Override
    public void setListener(ActivityMainBinding binding) {
        binding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    changeUI(CartoGramActivity.class);
                    finish();
                }
                if (position == 1) {
                    changeUI(CameraActivity.class);
                    finish();
                }
                if (position == 2) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    intent.setAction(Intent.ACTION_PICK);
                    intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //直接打开系统相册  不设置会有选择相册一步（例：系统相册、QQ浏览器相册）
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 200);
                }
            }
        });
    }

    @Override
    public boolean onBack() {
        return false;
    }

    List<File>fileList=new ArrayList<>();


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200 && data != null) {
            ClipData clipData = data.getClipData();
            if (clipData != null) {
                int itemCount = clipData.getItemCount();
                if (itemCount == 0) {
                    return;
                }
                for (int i = 0; i < itemCount; i++) {
                    ClipData.Item itemAt = clipData.getItemAt(i);
                    if (itemAt == null) continue;
                    Uri uri = itemAt.getUri();
                    String path = uri.getPath();
                    LogUtil.i("选中的照片路径:" + path);
                    fileList.add(new File(path));
                }
            }
        }
    }
}